import { createRouter, createWebHistory } from "vue-router";

//立即引入
// import User from "@/views/user/index.vue";
// console.log("立即引入 User", User);

const router = createRouter({
  history: createWebHistory(),

  //定义路由表

  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: () => import("@/views/Login.vue"),
    },
    {
      path: "/home",
      component: () => import("@/views/Home.vue"),
      children: [
        // {
        //   path: "/home/user",
        //   component: function () {
        //     console.log("懒加载   加载User");
        //     // return 组件;
        //     //Promise对象
        //     return import("@/views/user/index.vue");
        //   },
        // },

        {
          path: "/home/user",
          component: () => import("@/views/user/index.vue"),
        },
        {
          path: "/home/role",
          component: () => import("@/views/role/index.vue"),
        },
        {
          path: "/home/menu",
          component: () => import("@/views/menu/index.vue"),
        },
      ],
    },
  ],
});
let whiteList = ["/login"];

//在每次路由切换的时候 需要 做判断
router.beforeEach((to, from, next) => {
  console.log("路由切换了 ，判断是否登录 ,to.path", to);
  let username = sessionStorage.getItem("username");
  // let success: boolean = ;

  //如果没登录， 1 跳转登录页面、
  if (username || whiteList.indexOf(to.path) != -1) {
    next();
  } else {
    next("/login");
  }

  //如果已经登录 2 放行
});

export default router;
